layui.define(["form","jquery"],function(exports){
    var form = layui.form,
    $ = layui.jquery,
    Address = {
        //加载省数据
        provinces : function() {
            var proHtml = '',that = this;
            $.ajax({
                type:"POST",
                async:false,
                url:"/getDictAddressByPid",
                data:{"pid": '0'},
                dataType:"json",
                success:function (res) {
                    for (var i = 0; i < res.data.length; i++) {
                        proHtml += '<option value="' + res.data[i].code + '">' + res.data[i].name + '</option>';
                    }
                    $("select[name=province]").append(proHtml);
                    form.render();
                    // 设置value时触发
                    $("select[name=province]").change(function() {
                        $("select[name=area]").html('<option value="">请选择县/区</option>');
                        var provinceKey = $("select[name=province]").attr("data-key");
                        that.citys(provinceKey);
                    });
                    // select时触发
                    form.on('select(province)', function (proData) {
                        $("select[name=area]").html('<option value="">请选择县/区</option>');
                        var value = proData.value;
                        if (value > 0) {
                            that.citys(res.data[$(this).index() - 1].id);
                        } else {
                            $("select[name=city]").attr("disabled", "disabled");
                        }
                    });
                }
            })
        },
        //加载市数据
        citys : function(citys) {
            var cityHtml = '<option value="">请选择市</option>',that = this;
            $.ajax({
                type:"POST",
                async:false,
                url:"/getDictAddressByPid",
                data:{"pid": citys},
                dataType:"json",
                success:function (res) {
                    for (var i = 0; i < res.data.length; i++) {
                        cityHtml += '<option value="' + res.data[i].code + '">' + res.data[i].name + '</option>';
                    }
                    $("select[name=city]").html(cityHtml).removeAttr("disabled");
                    form.render();
                    // 设置value时触发
                    $("select[name=city]").change(function () {
                        var city = $("select[name=city]").attr("data-key");
                        that.areas(city);
                    });
                    // select时触发
                    form.on('select(city)', function (proData) {
                        var value = proData.value;
                        if (value > 0) {
                            that.areas(res.data[$(this).index() - 1].id);
                        } else {
                            $("select[name=area]").attr("disabled", "disabled");
                        }
                    });
                }
            })
        },
        //加载县/区数据
        areas : function(areas) {
            var areaHtml = '<option value="">请选择县/区</option>';
            $.ajax({
                type:"POST",
                async:false,
                url:"/getDictAddressByPid",
                data:{"pid": areas},
                dataType:"json",
                success:function (res) {
                    for (var i = 0; i < res.data.length; i++) {
                        areaHtml += '<option value="' + res.data[i].code + '">' + res.data[i].name + '</option>';
                    }
                    $("select[name=area]").html(areaHtml).removeAttr("disabled");
                    form.render();

                }
            });
        }
    };
    exports("address",Address.provinces());
})